/*
 * @Description:
 * @Author: 崔堂袁
 * @Date: 2021-04-24 23:00:13
 * @LastEditTime: 2021-05-11 12:06:22
 */
const express = require('express')
const router = express.Router()

const DBHelper = require('../utils/DBHelper')
const sql = require('../sqlMap')

// -------------------------------------------用户登录验证------------------------------------
router.post('/userLogin', (req, res) => {
  const sqlStr = sql.user.find
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStr, [params.account], (err, result) => {
    if (err) {
      console.log('查询异常')
      res.json(err)
    }
    if (result[0]) {
      const dbPassword = result[0].password
      const inputPassword = params.password
      if (dbPassword !== inputPassword) {
        console.log('密码错误')
        res.json('errPassword')
      }
      if (dbPassword === inputPassword) {
        console.log('验证成功')
        res.send({ state: 'success', userName: result[0].userName, userAccount: result[0].account })
      }
    } else {
      console.log('账号不存在')
      res.json('errAccount')
    }
  })
  conn.end()
})

// ------------------------------------------注册增加用户-----------------------------
router.post('/userRegister/find', (req, res) => {
  const sqlStrFind = sql.user.find
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStrFind, [params.account], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      if (result[0]) res.send('notAllowRegister')
      else res.send('allowRegister')
    }
  })
  conn.end()
})
router.post('/userRegister/addUser', (req, res) => {
  const sqlStrAdd = sql.user.add
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStrAdd, [params.account, params.password], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send('success')
    }
  })
  conn.end()
})

// --------------------------------------修改用户名-----------------------
router.post('/updateUserName', (req, res) => {
  const sqlStrUpdateUserName = sql.user.updateUserName
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStrUpdateUserName, [params.newUserName, params.userAccount], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send('success')
    }
  })
  conn.end()
})
// ------------------修改密码-------------
router.post('/updatePassword/find', (req, res) => {
  const sqlStrFind = sql.user.find
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStrFind, [params.userAccount], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      if (result[0].password === params.oldPassword) {
        res.send('allowUpdatePassword')
      } else {
        res.send('errorPassword')
      }
    }
  })
  conn.end()
})
router.post('/updatePassword', (req, res) => {
  const sqlStrUpdateUserName = sql.user.updatePassword
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStrUpdateUserName, [params.newPassword, params.userAccount], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send('success')
    }
  })
  conn.end()
})

// ------------------保存场景--------------
router.post('/saveData', (req, res) => {
  const sqlStrSaveData = sql.userData.add
  const params = req.body
  // console.log(params)
  const conn = new DBHelper().getConn()
  conn.query(sqlStrSaveData, [params.userAccount, params.programName, params.strScene, params.strPhoto], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send(result)
    }
  })
  conn.end()
})
// ---------------------获取用户列表信息-------------
router.post('/getData', (req, res) => {
  const sqlStrGetData = sql.userData.find
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStrGetData, [params.userAccount], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send(result)
    }
  })
  conn.end()
})
// --------------------查询用户数据---------------
router.post('/findUserData', (req, res) => {
  const sqlStr = sql.userData.findIsSameName
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStr, [params.userAccount, params.programName], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      if (result.length) {
        res.send('existedProgramName')
      } else {
        res.send('allowToAdd')
      }
    }
  })
  conn.end()
})
// --------------------更新场景----------------
router.post('/updateUserData', (req, res) => {
  const sqlStr = sql.userData.update
  const params = req.body
  const conn = new DBHelper().getConn()
  conn.query(sqlStr, [params.strScene, params.strPhoto, params.userAccount, params.programName], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send(result)
    }
  })
  conn.end()
})
// --------------------用户删除数据-------------
router.post('/deleteData', (req, res) => {
  const sqlStrDeleteData = sql.userData.delete
  const params = req.body
  // console.log(params)
  const conn = new DBHelper().getConn()
  conn.query(sqlStrDeleteData, [params.userAccount, params.programName], (err, result) => {
    if (err) {
      res.json(err)
    } else {
      res.send(result)
    }
  })
  conn.end()
})

module.exports = router
